package com.hit.server.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.server.api.entity.SysCustomerEntity;
import com.hit.server.api.entity.SysProjectEntity;
import com.hit.server.api.dto.SysProjectQueryDTO;
import com.hit.server.api.dto.SysProjectSaveDTO;
import com.hit.server.api.dto.SysProjectUpdateDTO;
import com.hit.server.api.vo.SysProjectVO;
import com.hit.server.biz.mapper.SysCustomerMapper;
import com.hit.server.biz.mapper.SysProjectMapper;
import com.hit.server.api.service.SysProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

/**
 * 项目表
 *
 * @author kehanjiang
 * @date 2025-04-03 17:26:12
 */
@Service
@RequiredArgsConstructor
public class SysProjectServiceImpl extends ServiceImpl<SysProjectMapper, SysProjectEntity> implements SysProjectService {

    private final SysCustomerMapper sysCustomerMapper;

	private SysProjectVO convertToVO(Object o) {
        if (o instanceof SysProjectEntity) {
            SysProjectVO vo = new SysProjectVO();
            BeanUtil.copyProperties(o, vo);
			//设置其他参数
            Optional.ofNullable(vo.getCustomerId()).ifPresent(customerId -> {
                SysCustomerEntity customer = sysCustomerMapper.selectById(customerId);
                if (customer != null) {
                    vo.setCustomerName(customer.getName());
                }
            });
            return vo;
        }
        return null;
    }
	
	private void preCheck(SysProjectEntity entity, Boolean update) {
        Long id = entity.getId();
        //String name = entity.getName();
        if (update) {//更新操作
           Assert.notNull(id, "id不可为空！");
        } else {
           //Assert.hasText(name, "name不可为空！");
        }
    }

    @Override
    public Page<SysProjectVO> getSysProjectPage(Page page, SysProjectQueryDTO dto){
        SysProjectEntity entity = new SysProjectEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setName(null);
        final String name = dto.getName();
        LambdaQueryWrapper<SysProjectEntity> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.like(StringUtils.hasText(name), SysProjectEntity::getName, name);
        wrapper.orderByAsc(SysProjectEntity::getId);
        return (Page<SysProjectVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }
	
    @Override
    public SysProjectVO getSysProjectById(Long id){
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }
	
	
    @Override
    public Boolean saveSysProject(SysProjectSaveDTO dto){
        SysProjectEntity entity = new SysProjectEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, false);
        return this.save(entity);
    }
	
    @Override
    public Boolean updateSysProjectById(SysProjectUpdateDTO dto){
        SysProjectEntity entity = new SysProjectEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, true);
        return this.updateById(entity);
    }
	
    @Override
    public Boolean removeSysProjectBatchByIds(List<Long> ids){
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }
}